﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Jy123.BLL.Public;
using Jy123.Web.AppCode;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class AddModuleLevel : MgConfig
    {
        private List<Jy123.Model.Public_JType> JTypeList;
        private Dictionary<int, string> typeNameDic = new Dictionary<int, string>();
        private string[] strBigType = { "赛场", "俱乐部", "训练中心" };
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("ModuleLevel.aspx");
            GetJTypeNameList();
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void GetJTypeNameList()
        {
            JTypeList = new List<Jy123.Model.Public_JType>();
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            JTypeList = BLLJType.GetModelList(string.Empty);
            foreach (Jy123.Model.Public_JType type in JTypeList)
            {
                typeNameDic.Add(type.ID, type.JName);
            }
        }

        protected void LoadInitialValue()
        {
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            List<Jy123.Model.Public_Level> levelList = BLLLevel.GetModelList(string.Empty);
            addLevelRepeater.DataSource = levelList;
            addLevelRepeater.DataBind();
            addLevelRepeater.Visible = levelList.Count > 0;

            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("JFather IS NULL AND ( 1 =0");
            for (int i = 0; i < strBigType.Length; i++)
            {
                strWhere.Append(" or JName = '" + strBigType[i] + "'");
            }
            strWhere.Append(")");
            bigTypeDropDown.Items.Clear();
            List<Jy123.Model.Public_JType> listJType = BLLJType.GetModelList(strWhere.ToString());
            bigTypeDropDown.DataSource = listJType;
            bigTypeDropDown.DataTextField = "JName";
            bigTypeDropDown.DataValueField = "ID";
            bigTypeDropDown.DataBind();
            bigTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
            bigTypeDropDown_Change(bigTypeDropDown, EventArgs.Empty);
            levelDropDown.Items.Clear();
            for (int i = 1; i < 6; i++)
            {
                levelDropDown.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            levelDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Level level = e.Item.DataItem as Jy123.Model.Public_Level;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = level.ID.ToString();
                Label nameLabel = e.Item.FindControl("typeNameLabel") as Label;
                nameLabel.Text = typeNameDic[level.FJType.Value];
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            Jy123.Model.Public_Level level = BLLLevel.GetModel(id);
            if (e.CommandName.Equals("Edit"))
            {
                SetPageValue(level);
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLLevel.Delete(id);
                LoadInitialValue();
            }
        }

        protected void SetPageValue(Jy123.Model.Public_Level level)
        {
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            Jy123.Model.Public_JType JType = BLLJType.GetModel(level.FJType.Value);
            int fatherId = JType.JFather.Value;
            bigTypeDropDown.SelectedValue = fatherId.ToString();
            bigTypeDropDown_Change(bigTypeDropDown, EventArgs.Empty);
            typeDropDown.SelectedValue = level.FJType.ToString();
            levelNameTextBox.Text = level.LName;
            if (level.PhysicalFitness.HasValue)
            {
                fitnessTextBox.Text = level.PhysicalFitness.Value.ToString();
            }
            if (level.PeakPH.HasValue)
            {
                peakPHTextBox.Text = level.PeakPH.Value.ToString();
            }
            if (level.Moral.HasValue)
            {
                moralTextBox.Text = level.Moral.Value.ToString();
            }
            if (level.PeakMoral.HasValue)
            {
                peakMoralTextBox.Text = level.PeakMoral.Value.ToString();
            }
            if (level.Skill.HasValue)
            {
                skillTextBox.Text = level.Skill.Value.ToString();
            }
            if (level.PeakSkill.HasValue)
            {
                peakSkillTextBox.Text = level.PeakSkill.Value.ToString();
            }
            if (level.Liveness.HasValue)
            {
                livenessTextBox.Text = level.Liveness.Value.ToString();
            }
            if (level.PeakLive.HasValue)
            {
                peakLiveTextBox.Text = level.PeakLive.Value.ToString();
            }
            if (level.Exp.HasValue)
            {
                expTextBox.Text = level.Exp.Value.ToString();
            }
            if (level.PeakExp.HasValue)
            {
                peakExpTextBox.Text = level.PeakExp.Value.ToString();
            }
            if (level.RaceTimes.HasValue)
            {
                timesTextBox.Text = level.RaceTimes.Value.ToString();
            }
            if (level.Scope.HasValue)
            {
                scopeTextBox.Text = level.Scope.Value.ToString();
            }
            if (level.UpgradeMoney.HasValue)
            {
                moneyTextBox.Text = (Convert.ToInt32(level.UpgradeMoney.Value)).ToString();
            }
            if (level.Cost.HasValue)
            {
                costTextBox.Text = (Convert.ToInt32(level.Cost.Value)).ToString();
            }
            if (level.Income.HasValue)
            {
                incomeTextBox.Text = (Convert.ToInt32(level.Income.Value)).ToString();
            }
            if (level.Premium.HasValue)
            {
                premiumTextBox.Text = (Convert.ToInt32(level.Premium.Value)).ToString();
            }
            levelDropDown.SelectedValue = level.LevelNumber.ToString();
            actionTypeHidden.Value = "Update";
            idHidden.Value = level.ID.ToString();
            JTypeImage.Src = level.Model;
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Level level = new Jy123.Model.Public_Level();
                SetLevelValue(level);
                int idL = BLLLevel.Add(level);
                level.Model = CreateLogo(fileUploadBig.PostedFile, idL);
                BLLLevel.Update(level);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Level level = BLLLevel.GetModel(id);
                SetLevelValue(level);
                if (fileUploadBig.PostedFile != null)
                {
                    string strLogo = CreateLogo(fileUploadBig.PostedFile, id);

                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        level.Model = strLogo;
                    }

                }
                BLLLevel.Update(level);
            }
            LoadInitialValue();
            actionTypeHidden.Value = "Add";
            bigTypeDropDown.SelectedValue = "-1";
            bigTypeDropDown_Change(bigTypeDropDown, EventArgs.Empty);
            typeDropDown.SelectedValue = "-1";
            levelDropDown.SelectedValue = "-1";
            timesTextBox.Text = string.Empty;
            moneyTextBox.Text = string.Empty;
        }

        protected void bigTypeDropDown_Change(object sender, EventArgs e)
        {
            int fId = int.Parse(bigTypeDropDown.SelectedValue);
            typeDropDown.Items.Clear();
            if (fId > 0)
            {
                Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                string strWhere = "JFather = '" + fId.ToString() + "'";
                List<Jy123.Model.Public_JType> listJType = BLLJType.GetModelList(strWhere);
                typeDropDown.DataSource = listJType;
                typeDropDown.DataTextField = "JName";
                typeDropDown.DataValueField = "ID";
                typeDropDown.DataBind();
            }
            typeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
            ClubDiv.Visible = (fId == Convert.ToInt32(EnumConfig.JyType.Club_Main));
            PublicDiv.Visible = ((fId == Convert.ToInt32(EnumConfig.JyType.Club_Main)) || (fId == Convert.ToInt32(EnumConfig.JyType.Venues_Main)));
            TCenterDiv.Visible = (fId == Convert.ToInt32(EnumConfig.JyType.TCenter_Main));
            venuesDiv.Visible = (fId == Convert.ToInt32(EnumConfig.JyType.Venues_Main));
            EmptyControls();
        }

        private void EmptyControls()
        {
            fitnessTextBox.Text = string.Empty;
            moralTextBox.Text = string.Empty;
            skillTextBox.Text = string.Empty;
            timesTextBox.Text = string.Empty;
            scopeTextBox.Text = string.Empty;
            costTextBox.Text = string.Empty;
            incomeTextBox.Text = string.Empty;
            premiumTextBox.Text = string.Empty;
            levelNameTextBox.Text = string.Empty;
            moneyTextBox.Text = string.Empty;
        }

        private Jy123.Model.Public_Level SetLevelValue(Jy123.Model.Public_Level level)
        {
            level.FJType = int.Parse(typeDropDown.SelectedValue);
            level.LName = levelNameTextBox.Text;
            if (!string.IsNullOrEmpty(fitnessTextBox.Text))
            {
                level.PhysicalFitness = int.Parse(fitnessTextBox.Text);
            }
            if (!string.IsNullOrEmpty(moralTextBox.Text))
            {
                level.Moral = int.Parse(moralTextBox.Text);
            }
            if (!string.IsNullOrEmpty(skillTextBox.Text))
            {
                level.Skill = int.Parse(skillTextBox.Text);
            }

            if (!string.IsNullOrEmpty(peakPHTextBox.Text))
            {
                level.PeakPH = int.Parse(peakPHTextBox.Text);
            }
            if (!string.IsNullOrEmpty(peakMoralTextBox.Text))
            {
                level.PeakMoral = int.Parse(peakMoralTextBox.Text);
            }
            if (!string.IsNullOrEmpty(peakSkillTextBox.Text))
            {
                level.PeakSkill = int.Parse(peakSkillTextBox.Text);
            }

            if (!string.IsNullOrEmpty(livenessTextBox.Text))
            {
                level.Liveness = int.Parse(livenessTextBox.Text);
            }
            if (!string.IsNullOrEmpty(peakLiveTextBox.Text))
            {
                level.PeakLive = int.Parse(peakLiveTextBox.Text);
            }
            if (!string.IsNullOrEmpty(expTextBox.Text))
            {
                level.Exp = int.Parse(expTextBox.Text);
            }
            if (!string.IsNullOrEmpty(peakExpTextBox.Text))
            {
                level.PeakExp = int.Parse(peakExpTextBox.Text);
            }

            if (!string.IsNullOrEmpty(timesTextBox.Text))
            {
                level.RaceTimes = int.Parse(timesTextBox.Text);
            }
            if (!string.IsNullOrEmpty(scopeTextBox.Text))
            {
                level.Scope = int.Parse(scopeTextBox.Text);
            }
            if (!string.IsNullOrEmpty(costTextBox.Text))
            {
                level.Cost = int.Parse(costTextBox.Text);
            }
            if (!string.IsNullOrEmpty(incomeTextBox.Text))
            {
                level.Income = int.Parse(incomeTextBox.Text);
            }
            if (!string.IsNullOrEmpty(premiumTextBox.Text))
            {
                level.Premium = int.Parse(premiumTextBox.Text);
            }
            if (!string.IsNullOrEmpty(moneyTextBox.Text))
            {
                level.UpgradeMoney = decimal.Parse(moneyTextBox.Text);
            }
            level.LevelNumber = int.Parse(levelDropDown.SelectedValue);
            return level;
        }

        protected string CreateLogo(HttpPostedFile postFile, int id)
        {
            CreateDir createDir = new CreateDir();
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateLevelPath(CreateDir.LEVEL, id, DateTime.Now, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
